package com.db.dbcloud;

import com.db.utils.ButtonStyleUtil;
import com.db.utils.ErrorJSONUtil;
import com.db.utils.FinalHttpUtil;
import com.db.utils.GsonUtil;
import com.db.utils.ServiceUtil;
import com.db.utils.StringHandler;
import com.db.vo.User;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

public class SettingActivity extends FinalActivity {
	@ViewInject(id = R.id.setting_logout_btn, click = "clickLogout")private ImageButton logoutBtn;
	@ViewInject(id = R.id.setting_volume_text)private TextView volumeText;
	@ViewInject(id = R.id.setting_volume_progress)private ProgressBar volumeProgress;
	@ViewInject(id = R.id.setting_volume_layout, click = "clickVolume")private RelativeLayout volumeLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_setting);
		ButtonStyleUtil.setButtonFocusChanged(logoutBtn);
	}
	@Override
	protected void onResume() {
		super.onResume();
		ajaxGetVolume();
	}
	public void clickVolume(View v){
		ajaxGetVolume();
	}
	public void ajaxGetVolume(){
		FinalHttp finalHttp = FinalHttpUtil.getFinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("uid", MainActivity.user.getuId());
		params.put("password", MainActivity.user.getuPassword());
		String url = ServiceUtil.domainUrl + "/getuser";
		finalHttp.post(url, params, new AjaxCallBack<String>(){
			@Override
			public void onStart() {
				super.onStart();
				volumeProgress.setVisibility(View.INVISIBLE);
				volumeText.setText("正在获取容量...");
			}
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				if(ErrorJSONUtil.isError(t)){
					volumeText.setText("获取容量失败");
					Toast.makeText(SettingActivity.this, ErrorJSONUtil.getErrorMsg(t), Toast.LENGTH_LONG).show();
				}else{
					User user = GsonUtil.getGson().fromJson(t, User.class);
					String volumeNum = StringHandler.formatDouble((double)user.getuVolume() / 1024);
					String surplusVolumeNum = StringHandler.formatDouble((double)user.getSurplusVolume() / 1024);
					volumeText.setText("容量("+surplusVolumeNum+"GB/"+volumeNum+"GB)");
					volumeProgress.setProgress((int)(((double)user.getSurplusVolume() / user.getuVolume()) * 100));
					volumeProgress.setVisibility(View.VISIBLE);
				}
			}
			@Override
			public void onFailure(Throwable t, String strMsg) {
				super.onFailure(t, strMsg);
				volumeText.setText("获取容量失败");
				Toast.makeText(SettingActivity.this, "获取容量失败，请检查网络问题", Toast.LENGTH_LONG).show();
			}
		});
	}
	public void clickLogout(View v){
		Intent intent = new Intent(this.getParent(), LoginActivity.class);
		intent.putExtra("userid", MainActivity.user.getuId());
		this.getParent().startActivity(intent);
		this.getParent().finish();
	}
}
